package com.byit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.byit.domain.SysUser;
import com.byit.mapper.SysUserMapper;
import com.byit.service.ISysUserService;
import com.byit.utils.PageUtil;
import com.byit.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author wangx
 * @since 2018-12-11
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Override
    ////@Cacheable(cacheNames = "cache", key = "'cache:user:login'")
    public SysUser userLogin(String loginName) {
        return userMapper.userLogin(loginName);
    }

    @Override
    public Page<JSONObject> selectUserList(Page<SysUser> pageInfo, Map<String, Object> params) {

        List<JSONObject> list = userMapper.selectUserList(pageInfo, params);

        for (JSONObject obj : list) {
            Object roles = obj.get("roles");
            if (null != roles) {
                List<Long> roleIds = StringUtil.splitLong(roles.toString(), ",");
                obj.put("roles", roleIds);
            }
        }

        Page<JSONObject> page = PageUtil.transform(pageInfo);

        page.setRecords(list);

        return page;
    }

    @Override
    public void delUserById(Map<String, Object> param) {
        userMapper.delUserById(param);
    }

    @Override
    public boolean selectUserByUserName(String loginName) {
        int count = userMapper.selectUserByUserName(loginName);
        return (count == 0) ? false : true;
    }

    @Override
    public void updateUserpwById(Map<String, Object> params) {
        userMapper.updateUserpwById(params);
    }
}
